using System;
using System.Collections.Generic;
using System.Text;
using Csla.Data;
using Csla;
using Active.Core.DB;

namespace Active.Core
{
    [Serializable()]
    public class PayoutRecord : BusinessBase<PayoutRecord>
    {
        #region Business Methods

        // TODO: add your own fields, properties and methods
        private int _id;

        public int Id
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _id;
            }
        }

        string _description;
        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _description;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_description.Equals(value))
                {
                    _description = value;
                    PropertyHasChanged();
                }
            }
        }

        int _orderId;
        /// <summary>
        /// Gets or sets the order id.
        /// </summary>
        /// <value>The order id.</value>
        public int OrderId
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _orderId;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_orderId.Equals(value))
                {
                    _orderId = value;
                    PropertyHasChanged();
                }
            }
        }

        decimal _payout;
        /// <summary>
        /// Gets or sets the Payout.
        /// </summary>
        /// <value>The Payout.</value>
        public decimal Payout
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _payout;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_payout.Equals(value))
                {
                    _payout = value;
                    PropertyHasChanged();
                }
            }
        }

        SmartDate _payoutDate = new SmartDate(false);
        public SmartDate PayoutDate
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _payoutDate;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_payoutDate.Equals(value))
                {
                    _payoutDate = value;
                    PropertyHasChanged();
                }
            }
        }

        int _payoutUnitId;
        public int PayoutUnitId
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _payoutUnitId;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_payoutUnitId.Equals(value))
                {
                    _payoutUnitId = value;
                    PropertyHasChanged();
                }
            }
        }

        decimal _deductFromAdvance;
        public decimal DeductFromAdvance
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _deductFromAdvance;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_deductFromAdvance.Equals(value))
                {
                    _deductFromAdvance = value;
                    PropertyHasChanged();
                }
            }
        }

        decimal _deductFromBrokerage;
        public decimal DeductFromBrokerage
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _deductFromBrokerage;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_deductFromBrokerage.Equals(value))
                {
                    _deductFromBrokerage = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        internal static PayoutRecord NewPayoutRecord()
        {
            // TODO: change to use new keyword if not loading defaults
            return new PayoutRecord();
            //return DataPortal.Create<PayoutRecord>();
        }

        internal static PayoutRecord GetPayoutRecord(SafeDataReader dr)
        {
            return new PayoutRecord(dr);
        }

        private PayoutRecord()
        {
            MarkAsChild();
        }

        private PayoutRecord(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            _id = dr.GetInt32("Id");
            _deductFromAdvance = dr.GetDecimal("DeductFromAdvance");
            _deductFromBrokerage = dr.GetDecimal("DeductFromBrokerage");
            _description = dr.GetString("Description");
            _orderId = dr.GetInt32("OrderId");
            _payout = dr.GetDecimal("Payout");
            _payoutDate = dr.GetSmartDate("PayoutDate", false);
            _payoutUnitId = dr.GetInt32("PayoutUnitId");

            MarkOld();
        }

        internal void Insert(Order parent)
        {
            _orderId = parent.Id;

            NBearLite.InsertSqlSection insert =
                DataService.DBService.Insert(Tables.PayoutRecords)
                .AddColumn(Tables.PayoutRecords.DeductFromAdvance, _deductFromAdvance)
                .AddColumn(Tables.PayoutRecords.DeductFromBrokerage, _deductFromBrokerage)
                .AddColumn(Tables.PayoutRecords.Description, _description)
                .AddColumn(Tables.PayoutRecords.OrderId, _orderId)
                .AddColumn(Tables.PayoutRecords.Payout, _payout)
                .AddColumn(Tables.PayoutRecords.PayoutDate, _payoutDate.Date)
                .AddColumn(Tables.PayoutRecords.PayoutUnitId, _payoutUnitId);
            _id = insert.ExecuteReturnAutoIncrementID(Tables.PayoutRecords.Id);
            MarkOld();
        }

        internal void Update(Order parent)
        {
            _orderId = parent.Id;
            NBearLite.UpdateSqlSection update =
                DataService.DBService.Update(Tables.PayoutRecords)
                .AddColumn(Tables.PayoutRecords.DeductFromAdvance, _deductFromAdvance)
                .AddColumn(Tables.PayoutRecords.DeductFromBrokerage, _deductFromBrokerage)
                .AddColumn(Tables.PayoutRecords.Description, _description)
                .AddColumn(Tables.PayoutRecords.OrderId, _orderId)
                .AddColumn(Tables.PayoutRecords.Payout, _payout)
                .AddColumn(Tables.PayoutRecords.PayoutDate, _payoutDate.Date)
                .AddColumn(Tables.PayoutRecords.PayoutUnitId, _payoutUnitId)
                .Where(Tables.PayoutRecords.Id == _id);
            update.Execute();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            DataService.DBService.Delete(Tables.PayoutRecords)
                .Where(Tables.PayoutRecords.Id == _id).Execute();
            MarkNew();
        }

        #endregion
    }
}
